// lunarService.js
import {getLunar, Lunar, Solar} from 'chinese-lunar-calendar';
import { parseTime } from "@/utils/ruoyi";

import moment from 'moment';

/**
 * * 获取农历信息
 * @param {string} timeStr  或  new Date()
 * @returns {object} 或 {string}
 */
export function getLunarInfo(timeStr ,type) {
  const inDate = new Date(timeStr);
  let data = getLunar(inDate.getFullYear(), (inDate.getMonth() + 1), inDate.getDate());
  if(type === 'obj'){
    return data;
  }else {
    return data.lunarYear+data.dateStr;
  }
}

/**
 * * 获取农历信息的日期格式
 * @param {string} timeStr  或  new Date()
 * @returns {object} 或 {string}
 */
export function getLunarToDate(dateStr) {
  const date = new Date(dateStr);
  const lunarData = getLunar(date.getFullYear(), (date.getMonth() + 1), date.getDate());
  const  oldDate  = new Date(date.getFullYear(), (lunarData.lunarMonth-1), lunarData.lunarDate);
  return parseTime(oldDate, '{y}-{m}-{d} {h}:{m}:{s}');
}

/**
 * * 获取距离现在的时间差
 * @param {string} timeStr  或  new Date()
 * @returns {object} 或 {string}
 */
export function getUpToNowInfo(timeStr ,type) {
  const now = moment();
  const target = moment(timeStr);

  const duration = moment.duration(now.diff(target));
  if(type === 'obj'){
    return duration;
  }else {
    return duration.years()+'年'+ duration.months()+'个月'+ duration.days()+'天'+duration.hours()+'小时'+duration.minutes()+'分'+duration.seconds()+'秒';
  }

}

/**
 * * 获取距离现在的时间差 多少天
 * @param {string} timeStr  或  new Date()
 * @returns {object} 或 {string}
 */
export function getUpToNowDayInfo(timeStr ,type) {
  const now = moment();
  const target = moment(timeStr);

  const days = now.diff(target, 'days');
  const duration = moment.duration(now.diff(target));

  return days+'天'+duration.hours()+'小时'+duration.minutes()+'分'+duration.seconds()+'秒';

}


/**
 * 获取生辰八字
 * @param {string} birthDateTime - 出生年月日时分，格式为 'YYYY-MM-DD HH:mm'
 * @returns {string} 生辰八字
 */
export function getBirthGanzhi(birthDateTime) {
  const birthDate = new Date(birthDateTime);
  const lunar = getLunar(birthDate.getFullYear(), birthDate.getMonth() + 1, birthDate.getDate());
console.log('生辰八字',lunar)
  // 获取年干支
  const yearGanzhi = lunar.lunarYear;

  const dateStr = lunar.dateStr;


  // 获取时干支
  const hourGanzhi = getHourGanzhi(birthDate.getHours());

  return `${yearGanzhi}${dateStr}${hourGanzhi}`;
}

/**
 * 根据小时获取时干支
 * @param {number} hour - 小时
 * @returns {string} 时干支
 */
function getHourGanzhi(hour) {
  const ganzhi = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'];
  const startHour = 23; // 子时从23点开始
  const index = Math.floor((hour - startHour + 24) % 24 / 2);
  return ganzhi[index];
}


